<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //获取0-9之间的随机整数
        var arr = [];
        for(var i=0;i<4;i++){
            var index = Math.floor(Math.random()*10);//[0,9]
            if(arr.indexOf(index)==-1){
                arr.push(index);
            }else{
                i--;
            }
        }
        document.write(arr);


        














        function getIndex() {
            return Math.floor(Math.random() * 10);
        }
        var arr = [];
        for (var i = 0; i < 4; i++) {
            var index = getIndex();
            if (arr.indexOf(index) == -1) {
                arr.push(index);
            } else {
                i--;
            }
        }
        document.write(arr);
    </script>

</body>

</html>